package validaciones;

import general.CCo000;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.Image;
import java.io.IOException;
import java.sql.*;
import java.io.ByteArrayInputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.JLabel;
import utilitario.CImagen;

public class CLogin{

    private String usuarioLogin = null;
    private String claveLogin = null;
    private String sucursalActual = null;
    private String companiaActual = null;
    private String conectorBase = null;
    private String fechaLogin = null;
    private String horaLogin = null;

    public CLogin() {
        try {
            this.setUsuarioLogin();
            this.setClaveLogin();
            this.setSucursalActual();
            this.setCompaniaActual();
            this.setConector();
            this.setFechaLogin();
            this.setHoraLogin();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CLogin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @return the usuarioLogin
     */
    public String getUsuarioLogin() {
        return usuarioLogin;
    }

    /**
     * @param usuarioLogin the usuarioLogin to set
     */
    private void setUsuarioLogin() throws IOException {
        FileReader lectorArchivo = new FileReader("C:\\windows\\Temp\\claves\\pass.txt");
        BufferedReader entrada = new BufferedReader(lectorArchivo);
        String linea = "";
        linea = entrada.readLine();
        linea = linea.substring(9);
        this.usuarioLogin = linea;
        entrada.close();
        lectorArchivo.close();
    }

    /**
     * @return the claveLogin
     */
    public String getClaveLogin() {
        return claveLogin;
    }

    /**
     * @param claveLogin the claveLogin to set
     */
    private void setClaveLogin() throws IOException {
        FileReader lectorArchivo = new FileReader("C:\\windows\\Temp\\claves\\pass.txt");
        BufferedReader entrada = new BufferedReader(lectorArchivo);
        String linea = "";
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = linea.substring(6);
        this.claveLogin = linea;
        entrada.close();
        lectorArchivo.close();
    }

    /**
     * @return the sucursalActual
     */
    public String getSucursalActual() {
        return sucursalActual;
    }

    /**
     * @param sucursalActual the sucursalActual to set
     */
    private void setSucursalActual() throws IOException {
        FileReader lectorArchivo = new FileReader("C:\\windows\\Temp\\claves\\pass.txt");
        BufferedReader entrada = new BufferedReader(lectorArchivo);
        String linea = "";
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = linea.substring(10);
        this.sucursalActual = linea;
        entrada.close();
        lectorArchivo.close();
    }

    /**
     * @return the companiaActual
     */
    public String getCompaniaActual() {
        return companiaActual;
    }

    /**
     * @param companiaActual the companiaActual to set
     */
    private void setCompaniaActual() throws IOException {
        FileReader lectorArchivo = new FileReader("C:\\windows\\Temp\\claves\\pass.txt");
        BufferedReader entrada = new BufferedReader(lectorArchivo);
        String linea = "";
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = linea.substring(10);
        this.companiaActual = linea;
        entrada.close();
        lectorArchivo.close();
    }

    public String getConectorBase() {
        return this.conectorBase;
    }

    private void setConector() throws FileNotFoundException, IOException {
        FileReader lectorArchivo = new FileReader("C:\\windows\\Temp\\claves\\pass.txt");
        BufferedReader entrada = new BufferedReader(lectorArchivo);
        String linea = "";
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = linea.substring(10);
        this.conectorBase = linea;
        entrada.close();
        lectorArchivo.close();
    }

    /**
     * @return the fechaLogin
     */
    public String getFechaLogin() {
        return fechaLogin;
    }

    /**
     * @param fechaLogin the fechaLogin to set
     */
    private void setFechaLogin() throws IOException {
        FileReader lectorArchivo = new FileReader("C:\\windows\\Temp\\claves\\pass.txt");
        BufferedReader entrada = new BufferedReader(lectorArchivo);
        String linea = "";
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        this.fechaLogin = linea;
        entrada.close();
        lectorArchivo.close();
    }

    /**
     * @return the horaLogin
     */
    public String getHoraLogin() {
        return horaLogin;
    }

    /**
     * @param horaLogin the horaLogin to set
     */
    private void setHoraLogin() throws IOException {
        FileReader lectorArchivo = new FileReader("C:\\windows\\Temp\\claves\\pass.txt");
        BufferedReader entrada = new BufferedReader(lectorArchivo);
        String linea = "";
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        linea = entrada.readLine();
        this.horaLogin = linea;
        entrada.close();
        lectorArchivo.close();
    }

    public void cargarImagen(JLabel label) throws IOException, ClassNotFoundException, SQLException {
        CCo000 co = new CCo000();
        CImagen imagen = new CImagen();
        try {
            label.setText("");
            byte[] imagenBytes = co.getImagen();
            ByteArrayInputStream bis = new ByteArrayInputStream(imagenBytes);
            Iterator readers = ImageIO.getImageReadersByFormatName("jpeg");
            ImageReader reader = (ImageReader) readers.next();
            Object source = bis; // File or InputStream
            ImageInputStream iis = ImageIO.createImageInputStream(source);
            reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();
            Image ima = reader.read(0, param);
            GraphicsConfiguration gc = imagen.getDefaultConfiguration();
            BufferedImage image = imagen.toCompatibleImage(imagen.toBufferedImage(ima), gc);
            double escala;
            if (image.getWidth() > 150) {
                escala = 150 / ((double) image.getWidth());
            } else {
                escala = 150 / ((double) image.getHeight());
            }
            final double SCALE = escala;
            int w = (int) (SCALE * image.getWidth());
            int h = (int) (SCALE * image.getHeight());
            final BufferedImage resize = imagen.getScaledInstance(image, w, h, gc);
            label.setIcon(new ImageIcon(resize));
        } catch (java.lang.NullPointerException ex) {
            System.out.print("No tiene Logo");
            label.setIcon(null);
        }
    }
    
    public void escribeArchivo(File file, String usu, String pass, String sucu, String cia) throws IOException {
        File dir = new File("C:\\windows\\Temp\\claves");
        dir.mkdirs();
        PrintWriter fileOut = new PrintWriter(new FileWriter(file));
        fileOut.print("USUARIO: ");
        fileOut.println(usu);
        fileOut.print("PASS: ");
        fileOut.println(pass);
        fileOut.print("SUCURSAL: ");
        fileOut.println(sucu);
        fileOut.print("COMPANIA: ");
        fileOut.println(cia);
        fileOut.print("SERVIDOR: ");
        fileOut.println(conectorBase);
        fileOut.println(fechaLogin);
        fileOut.println(horaLogin);
        fileOut.close();
        
    }

       
}
